﻿using System;
using System.Collections.Generic;
using System.Text;

namespace curbside.Web.UI.WebControls
{
    /// <summary>
    /// Iframe控件
    /// </summary>
    public class Iframe : System.Web.UI.WebControls.WebControl
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.Write("<iframe id=\"" + this.ClientID
                + "\" src=\"" + this.Src
                + "\" frameborder=\"" + this.FrameBorder
                + "\" width=\"" + this.Width
                + "\" height=\"" + this.Height
                + "\" scrolling=\"" + this.Scrolling + "\""
                + "></iframe>");

            string jsEvent = "$(function(){var jobj = $('#" + this.ClientID + "');";
            string jsCode = string.Empty;

            if (AutoGetParentParams || AutoAdaptHeight || ReAdaptWhenDomChanged)
            {
                jsCode += "jobj.iframeExtend({"
                    + "autoGetParentParams:" + AutoGetParentParams.ToString().ToLower() + ","
                    + "adaptHeight:" + AutoAdaptHeight.ToString().ToLower() + ","
                    + "reAdaptWhenDomChanged:" + ReAdaptWhenDomChanged.ToString().ToLower()
                    + "});";
            }

            if (!string.IsNullOrEmpty(jsCode))
            {
                jsEvent += jsCode + "});";
                helper.AddStartupScript(ClientID, jsEvent, this.Page);
            }
        }

        /// <summary>
        /// 设置或获取 iframe src地址
        /// </summary>
        public string Src
        {
            get
            {
                string str = this.ViewState["Src"] as string;
                if (str != null) return str;
                return string.Empty;
            }
            set { this.ViewState["Src"] = value; }
        }

        /// <summary>
        /// 设置或获取 iframe frameborder
        /// 默认值为0
        /// </summary>
        public int FrameBorder
        {
            get
            {
                object obj = this.ViewState["FrameBorder"];
                if (obj == null) return 0;
                return (int)obj;
            }
            set { this.ViewState["FrameBorder"] = value; }
        }

        /// <summary>
        /// 设置或获取 是否自动获取父窗口参数
        /// 默认值为false
        /// </summary>
        public bool AutoGetParentParams
        {
            get
            {
                object obj = this.ViewState["AutoGetParentParams"];
                if (obj == null) return false;
                return (bool)obj;
            }
            set { this.ViewState["AutoGetParentParams"] = value; }
        }

        /// <summary>
        /// 设置获取 是否自动调整iframe高度
        /// 默认值为 true
        /// </summary>
        public bool AutoAdaptHeight
        {
            get
            {
                object obj = this.ViewState["AutoAdaptHeight"];
                if (obj == null) return true;
                return (bool)obj;
            }
            set { this.ViewState["AutoAdaptHeight"] = value; }
        }

        /// <summary>
        /// 设置获取 当页面元素变化时是否自动调整iframe高度
        /// 默认值为 true
        /// </summary>
        public bool ReAdaptWhenDomChanged
        {
            get
            {
                if (!AutoAdaptHeight) return false;
                object obj = this.ViewState["ReAdaptWhenDomChanged"];
                if (obj == null) return true;
                return (bool)obj;
            }
            set { this.ViewState["ReAdaptWhenDomChanged"] = value; }
        }

        /// <summary>
        /// 设置或获取 iframe滚动方式
        /// </summary>
        public ScrollType Scrolling
        {
            get
            {
                object obj = this.ViewState["Scrolling"];
                if (obj == null) return ScrollType.auto;
                return (ScrollType)obj;
            }
            set { this.ViewState["Scrolling"] = value; }
        }

        /// <summary>
        /// 定义iframe滚动枚举
        /// </summary>
        public enum ScrollType
        {
            auto,
            yes,
            no
        }
    }
}
